_base_ = [
    '/home/yangshuo/past_comp/DPLABV3/mmsegmentation/configs/_base_/models/deeplabv3plus_r50-d8.py',
    # '/home/yangshuo/past_comp/DPLABV3/mmsegmentation/configs/_base_/datasets/ali_train_512x512.py',
    # '/home/yangshuo/past_comp/DPLABV3/mmsegmentation/configs/_base_/datasets/whu_aeria_512x512.py',
    '/home/yangshuo/past_comp/DPLABV3/mmsegmentation/configs/_base_/default_runtime.py',
    '/home/yangshuo/past_comp/DPLABV3/mmsegmentation/configs/_base_/schedules/schedule_20k.py']


norm_cfg = dict(type='SyncBN', requires_grad=True)
CLASSES = ('background','building')
PALETTE = [[0,0,0],[255,255,255]]

model = dict(
    type='EncoderDecoder',
    pretrained='open-mmlab://resnet50_v1c',
    backbone=dict(
        type='ResNetV1c',
        depth=50,
        num_stages=4,
        out_indices=(0, 1, 2, 3),
        dilations=(1, 1, 2, 4),
        strides=(1, 2, 1, 1),
        norm_cfg=dict(type='BN', requires_grad=True),
        norm_eval=False,
        style='pytorch',
        contract_dilation=True),
    decode_head=dict(
        type='DepthwiseSeparableASPPHead',
        in_channels=2048,
        in_index=3,
        channels=512,
        dilations=(1, 12, 24, 36),
        c1_in_channels=256,
        c1_channels=48,
        dropout_ratio=0.1,
        num_classes=len(CLASSES),
        norm_cfg=dict(type='BN', requires_grad=True),
        align_corners=False,
        loss_decode=dict(
            type='CrossEntropyLoss', use_sigmoid=False, loss_weight=1.0,
            class_weight=[0.9, 1.1])),
    auxiliary_head=dict(
        type='FCNHead',
        in_channels=1024,
        in_index=2,
        channels=256,
        num_convs=1,
        concat_input=False,
        dropout_ratio=0.1,
        num_classes=len(CLASSES),
        norm_cfg=dict(type='BN', requires_grad=True),
        align_corners=False,
        loss_decode=dict(
            type='CrossEntropyLoss', use_sigmoid=False, loss_weight=0.4,
            class_weight=[0.9, 1.1])),
    train_cfg=dict(),
    test_cfg=dict(mode='whole'))

img_norm_cfg = dict(
    mean=[123.675, 116.28, 103.53], std=[58.395, 57.12, 57.375], to_rgb=True)
crop_size = (512,512)
train_pipeline = [
    dict(type='LoadImageFromFile'),
    dict(type='LoadAnnotations'),
    dict(type='Resize', img_scale=(512,512), ratio_range=(0.5, 2.0)),
    dict(type='RandomCrop', crop_size=crop_size, cat_max_ratio=0.75),
    dict(type='RandomFlip', prob=0.5),
    dict(type='PhotoMetricDistortion'),
    dict(type='Normalize', **img_norm_cfg),
    dict(type='Pad', size=crop_size, pad_val=0, seg_pad_val=255),
    dict(type='DefaultFormatBundle'),
    dict(type='Collect', keys=['img', 'gt_semantic_seg']),
]
test_pipeline = [
    dict(type='LoadImageFromFile'),
    dict(
        type='MultiScaleFlipAug',
        img_scale=(512,512),
        # img_ratios=[0.5, 0.75, 1.0, 1.25, 1.5, 1.75],
        flip=False,
        transforms=[
            dict(type='Resize', keep_ratio=True),
            dict(type='RandomFlip'),
            dict(type='Normalize', **img_norm_cfg),
            dict(type='ImageToTensor', keys=['img']),
            dict(type='Collect', keys=['img']),
        ])
]

dataset_ali = dict(
    type = 'Ali_2classes' , 
    data_root='/data/yangshuo/DPLABV3P/comp_Ali_building_2class',
    img_dir= 'train',
    ann_dir= 'train_gt_0_1',
    pipeline=train_pipeline,
    split='splits/train.txt'
)
# dataset_whu = dict(
#     type = 'WHU_Aeria' ,
#     data_root='/data/yangshuo/DPLABV3P/WHU_building/WHU_AerialImageDataset/work',
#     img_dir= 'train',
#     ann_dir= 'gt',
#     pipeline=train_pipeline,
#     split='splits/train.txt'
# )


data = dict(
    samples_per_gpu=12,
    workers_per_gpu=2,
    train= [dataset_ali  ] , 
    val=dict(
        type = 'Ali_2classes' , 
        data_root= '/data/yangshuo/DPLABV3P/comp_Ali_building_2class' , 
        img_dir='train' , 
        ann_dir= 'train_gt_0_1' , 
        pipeline=test_pipeline,
        split='splits/val.txt'),
    test=dict(
        type = 'Ali_2classes' , 
        data_root= '/data/yangshuo/DPLABV3P/comp_Ali_building_2class' , 
        img_dir='train' , 
        ann_dir='train_gt_0_1', 
        pipeline=test_pipeline,
        split='splits/val.txt'))



work_dir = '/home/yangshuo/past_comp/DPLABV3/code/3_Aug_stage/work_dir/deeplabv3p_balanced_class'
load_from = '/home/yangshuo/past_comp/DPLABV3/code/3_Aug_stage/weight/iter_15000.pth'



# optimizer = dict(type='SGD', lr=3e-5, momentum=0.9, weight_decay=0.0005)
# # learning policy
# lr_config = dict(policy='poly', power=0.9, min_lr=5e-6, by_epoch=False)
optimizer = dict(
    _delete_=True,
    type='AdamW',
    lr=3e-5,
    betas=(0.9, 0.999),
    weight_decay=0.01,
    paramwise_cfg=dict(
        custom_keys={
            'head': dict(lr_mult=10.) , 
            'absolute_pos_embed': dict(decay_mult=0.),
            'relative_position_bias_table': dict(decay_mult=0.),
            'norm': dict(decay_mult=0.)
        }))

lr_config = dict(
    _delete_=True,
    policy='poly',
    warmup='linear',
    warmup_iters=1500,
    warmup_ratio=1e-6,
    power=1.0,
    min_lr=1e-6,
    by_epoch=False)



runner = dict(type='IterBasedRunner', max_iters=20000)
evaluation = dict(interval=500, metric='mIoU', pre_eval=True)
checkpoint_config = dict(
    by_epoch=False, 
    interval=500, 
    max_keep_ckpts=3,
    save_last = True)
log_config = dict(
    interval=10,
    hooks=[dict(type='TextLoggerHook'),
           dict(type='TensorboardLoggerHook')])
gpu_ids = range(4)
